package com.ruoyi.domain;

import lombok.Data;

import java.security.SecureRandom;
import java.util.Base64;


public class InviteCode {
    public static final String CHARS= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int CODE_LENGTH = 8;
    private static final SecureRandom SECURE_RANDOM = new SecureRandom();
    public static String getInviteCode() {
        byte[] randomBytes = new byte[CODE_LENGTH];
        SECURE_RANDOM.nextBytes(randomBytes);
        // 使用Base64编码确保输出的邀请码只包含CHARS中的字符
        String code = Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
        // 从Base64编码的字符串中截取前8位字符
        return code.substring(0, CODE_LENGTH);
    }
}
